//
//  FindPhoneView.m
//  project
//
//  Created by 圆圆陈 on 2018/3/7.
//  Copyright © 2018年 黄建华. All rights reserved.
//

#import "FindPhoneView.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AudioToolbox/AudioToolbox.h>

@interface FindPhoneView()

@property(nonatomic,strong)AVAudioPlayer *player;
@property(nonatomic,strong)UISlider *volumeViewSlider;

@end

@implementation FindPhoneView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width-FitW(100), FitH(200))];
        backView.center = CGPointMake(frame.size.width/2, frame.size.height/2);
        backView.backgroundColor = SNCOLORWHITE;
        backView.layer.cornerRadius = 10;
        backView.clipsToBounds = YES;
        [self addSubview:backView];
        CGFloat width = backView.width;
        CGFloat height = backView.height;
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake((width-FitW(60))/2, FitH(10), FitW(60),height/3)];
        [imageView setImage:UIImageNamed(@"手机震动")];
        [backView addSubview:imageView];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, width, height/3)];
        label.center = CGPointMake(width/2, height/2);
        label.text = SN_Text(@"手机震动中......");
        label.numberOfLines = 0;
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = UIColorRGB(72, 72, 72);
        [backView addSubview:label];
        
        UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(0, height*2/3, width, height/3)];
        [button setTitleNormal:SN_Text(@"确定")];
        [button setTitleColorNormal:UIColorRGB(0, 0, 0)];
        [button addTouchUpTarget:self action:@selector(btnClick)];
        [backView addSubview:button];
        
        [self playMusic];
    }
    return self;
}

-(void)playMusic{
    NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"查找手机铃声（循环）" ofType:@"wav"]];
    _player = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
    [_player prepareToPlay];
    [_player setVolume:0.5];
    _player.numberOfLoops = 1; //设置音乐播放次数  -1为一直循环
    [_player play]; //播放
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(playStopWithTime) object:nil];
    [self performSelector:@selector(playStopWithTime) withObject:nil afterDelay:10.0];
}

- (void)playStopWithTime
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(playStopWithTime) object:nil];
    [_player stop];
}

-(void)btnClick{
    [_player stop];
    [self animationedDown];
    [self playStopWithTime];
}

- (void)stop
{
    [_player stop];
    [self playStopWithTime];
}

-(void)tapTouch{
    [_player stop];
    [self animationedDown];
    [self playStopWithTime];
}

-(void)remoteControlReceivedWithEvent:(UIEvent *)event{
    
}

@end
